"""
  此文件对提取的换道数据进行测试（测试s、v、acc，x横向）
"""

# !处理路径导入问题（添加绝对路径）！！！
import sys
import os
CODE_INTERNAL_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')) # 生成Code文件夹内部对应的绝对路径
sys.path.append(CODE_INTERNAL_PATH)

# 导入内部包
from utils.read_data import read_extract_lanechange_data
from utils.draw_data import draw_line_chart

# 文件路径常量
FILE_PATH_I80_1_to = "../../../Data/Ngsim数据集/I80数据集/3. 提取数据/2. 换道数据/trajectories-0400-0415_lanechange.txt" # 300 80 21 / 过滤非法数据后 93 80 21（师兄原始的实验数据量 260 297 14）
FILE_PATH_I80_2_to = "../../../Data/Ngsim数据集/I80数据集/3. 提取数据/2. 换道数据/trajectories-0500-0515_lanechange.txt" # 251 80 21 / 过滤非法数据后 74 80 21（师兄原始的实验数据量 212 297 14）
FILE_PATH_I80_3_to = "../../../Data/Ngsim数据集/I80数据集/3. 提取数据/2. 换道数据/trajectories-0515-0530_lanechange.txt" # 281 80 21 / 过滤非法数据后 71 80 21（师兄原始的实验数据量 211 297 14）
FILE_PATH_101_1_to = "../../../Data/Ngsim数据集/101数据集/3. 提取数据/2. 换道数据/trajectories-0750am-0805am_lanechange.txt" # 293 80 21 / 过滤非法数据后 150 80 21（师兄原始的实验数据量 215 297 14）
FILE_PATH_101_2_to = "../../../Data/Ngsim数据集/101数据集/3. 提取数据/2. 换道数据/trajectories-0805am-0820am_lanechange.txt" # 300 80 21 / 过滤非法数据后 161 80 21（师兄原始的实验数据量 242 297 14）
FILE_PATH_101_3_to = "../../../Data/Ngsim数据集/101数据集/3. 提取数据/2. 换道数据/trajectories-0820am-0835am_lanechange.txt" # 326 80 21 / 过滤非法数据后 139 80 21（师兄原始的实验数据量 227 297 14）

def extract_test_data(one_car_data, front_s_idx=7, self_s_idx=8, after_s_idx=9, front_x_idx=4, self_x_idx=5, after_x_idx=6, front_v_idx=10, self_v_idx=11, after_v_idx=12, front_acc_idx=13, self_acc_idx=14, after_acc_idx=15, front_self_distance_idx=17, self_after_distance_idx=18):
  # 提取所有s值
  front_s = [row[front_s_idx] for row in one_car_data]
  self_s = [row[self_s_idx] for row in one_car_data]
  after_s = [row[after_s_idx] for row in one_car_data]

  # 提取所有x值（横向位置）
  front_x = [row[front_x_idx] for row in one_car_data]
  self_x = [row[self_x_idx] for row in one_car_data]
  after_x = [row[after_x_idx] for row in one_car_data]

  # 提取所有v值
  front_v = [row[front_v_idx] for row in one_car_data]
  self_v = [row[self_v_idx] for row in one_car_data]
  after_v = [row[after_v_idx] for row in one_car_data]

  # 提取所有acc值
  front_acc = [row[front_acc_idx] for row in one_car_data]
  self_acc = [row[self_acc_idx] for row in one_car_data]
  after_acc = [row[after_acc_idx] for row in one_car_data]

  # 提取所有车头间距值
  front_self_distance = [row[front_self_distance_idx] for row in one_car_data]
  self_after_distance = [row[self_after_distance_idx] for row in one_car_data]

  return front_s, self_s, after_s, front_x, self_x, after_x, front_v, self_v, after_v, front_acc, self_acc, after_acc, front_self_distance, self_after_distance

def draw_chart(front_s, self_s, after_s, front_x, self_x, after_x, front_v, self_v, after_v, front_acc, self_acc, after_acc, front_self_distance, after_self_distance):
  data_x = [i for i in range(len(front_s))]

  # 绘制s
  datas_y = [{"data":front_s, "des":"头车车辆位置"}, {"data":self_s, "des":"自车车辆位置"}, {"data":after_s, "des":"后车车辆位置"}]
  labels = {"x":"时间（单位:0.1s）", "y":"位置（单位：m）"}
  draw_line_chart(data_x, datas_y, labels, "车辆纵向坐标")

  # 绘制x
  datas_y = [{"data":front_x, "des":"头车车辆位置"}, {"data":self_x, "des":"自车车辆位置"}, {"data":after_x, "des":"后车车辆位置"}]
  labels = {"x":"时间（单位:0.1s）", "y":"位置（单位：m）"}
  draw_line_chart(data_x, datas_y, labels, "车辆横向坐标")

  # 绘制v
  datas_y = [{"data":front_v, "des":"头车车辆速度"}, {"data":self_v, "des":"自车车辆速度"}, {"data":after_v, "des":"后车车辆速度"}]
  labels = {"x":"时间（单位:0.1s）", "y":"速度（单位：m/s）"}
  draw_line_chart(data_x, datas_y, labels, "车辆速度")

  # 绘制acc
  datas_y = [{"data":front_acc, "des":"头车车辆加速度"}, {"data":self_acc, "des":"自车车辆加速度"}, {"data":after_acc, "des":"后车车辆加速度"}]
  labels = {"x":"时间（单位:0.1s）", "y":"加速度（单位：m/s^2）"}
  draw_line_chart(data_x, datas_y, labels, "车辆加速度")

  # 绘制车头间距
  datas_y = [{"data":front_self_distance, "des":"头车与自车间距"}, {"data":self_after_distance, "des":"自车与后车间距"}]
  labels = {"x":"时间（单位:0.1s）", "y":"车头间距（单位：m）"}
  draw_line_chart(data_x, datas_y, labels, "车头间距")
  

if __name__ == "__main__":
  # 读取数据
  lanechange_datas = read_extract_lanechange_data(FILE_PATH_101_1_to)

  # 取一组测试数据
  test_data = lanechange_datas[40]

  # 提取测试数据
  front_s, self_s, after_s, front_x, self_x, after_x, front_v, self_v, after_v, front_acc, self_acc, after_acc, front_self_distance, self_after_distance = extract_test_data(test_data)

  # 绘图
  draw_chart(front_s, self_s, after_s, front_x, self_x, after_x, front_v, self_v, after_v, front_acc, self_acc, after_acc, front_self_distance, self_after_distance)